﻿using ComponentBuilder;
using ComponentBuilder.Parameters;

namespace BlamanticUI.Components;
/// <summary>
/// Represents one step in <see cref="StepGroup"/> component.
/// </summary>
[CssClass("step", Order = 999)]
public class Step : BlazorChildComponentBase<StepGroup, Step>,IHasOnDisabled,IHasChildContent
{
    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="Step"/> is disabled.
    /// </summary>
    [Parameter] public bool Disabled { get; set; }
    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="Step"/> is completed.
    /// </summary>
    /// <value>
    ///   <c>true</c> if completed; otherwise, <c>false</c>.
    /// </value>
    [Parameter] [CssClass("completed")] public bool Completed { get; set; }
    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    [Parameter] public string Title { get; set; }
    /// <summary>
    /// Gets or sets the description.
    /// </summary>
    [Parameter] public string Description { get; set; }
    /// <summary>
    /// Gets or sets the icon class.
    /// </summary>
    [Parameter] public string IconClass { get; set; }
    /// <summary>
    /// Gets or sets the color of the icon.
    /// </summary>
    [Parameter] public Color? IconColor { get; set; }
    /// <summary>
    /// Perform an action to disable component state.
    /// </summary>
    [Parameter]public EventCallback<bool> OnDisabled { get; set; }
    /// <summary>
    /// A segment of UI content to render.
    /// </summary>
    [Parameter]public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// Overrides to build CSS class by special logical process.
    /// </summary>
    /// <param name="builder">The instance of <see cref="T:ComponentBuilder.Abstrations.ICssClassBuilder" />.</param>
    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        builder.Append("link", ParentComponent.ClickToActive);
    }

    /// <summary>
    /// Renders the component to the supplied <see cref="T:Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder" />.
    /// </summary>
    /// <param name="builder">A <see cref="T:Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder" /> that will receive the render output.</param>
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        if (ChildContent is null)
        {
            builder.OpenElement(0, "div");
            BuildComponentAttributes(builder, out int sequence);
            //if (Parent.ClickToActive)
            //{
            //    AddClickToActiveAttribute(builder, 2);
            //}
            builder.AddContent(sequence+ 1, child =>
            {
                child.CreateComponent<Icon>(0, attributes: new { IconClass, Color = IconColor }, condition: !string.IsNullOrEmpty(IconClass));

                child.CreateComponent<Content>(10, content =>
                {
                    content.CreateElement(0, "div", Title, new { @class = "title" });
                    content.CreateComponent<Description>(10, Description, condition: !string.IsNullOrEmpty(Description));
                });
            });
            builder.CloseElement();
        }
        else
        {
            base.BuildRenderTree(builder);
        }
    }
}
